import {Component, ElementRef} from '@angular/core';
import {IonicPage, NavController, ToastController, NavParams, Keyboard,AlertController} from 'ionic-angular';
import {UnloadApi} from '../../api/unload/UnloadApi';
import { BarcodeScanner } from '@ionic-native/barcode-scanner';

@IonicPage()
@Component({
  selector: 'unload-materiel',
  templateUrl: 'unload-materiel.html'
})
export class UnloadMateriel {
  private projectId: string;
  private planPositionId: string;
  private planId: string;
  private containerId: string;
  private qrcode: string;
  private takePhoto: string;
  private materielAndcontainerInfo: object;
  private containerbearweightNow: string;  //已装载重量
  private MaterielvolumeSum: string;  //已经装载体积
  private disTag: boolean;
  private bizTag: boolean;
  private LeaveTag: boolean = true;

  constructor(private alertCtrl: AlertController,private barcodeScanner:BarcodeScanner,private navParams: NavParams, private UnloadApi: UnloadApi, private toastCtrl: ToastController, public navCtrl: NavController, private ele: ElementRef) {
    this.materielAndcontainerInfo = {
      materielId: "",
      materielCode: "",
      materielName: "",
      materielEmptyWeight: "",
      materielGrossWeight: "",
      materielLength: "",
      materielWidth: "",
      materielHeight: "",
      materielNumberIn: "",
      materielNumberOut: "",
      containerID: "",
      containerName: "",
      containerCode: "",
      containerType: "",
      containerLong: "",
      containerWide: "",
      containerHigh: "",
      containerDeadWeight: "",
      containerBearWeight: "",
      containerPermission: "",
      containerComment: ""
    }
    this.planPositionId = navParams.get("planPositionId");
    this.projectId = navParams.get("projectId");
    this.planId = navParams.get("planId");
    this.containerId = navParams.get("containerId");
    this.disTag = true;
    this.bizTag=false;
  }

  toPhoto(){
    this.navCtrl.push("ForcedPhoto",{
      "qrcode": this.qrcode,
      "planPositionId": this.planPositionId,
      "bizTag": this.bizTag
    });
  }


//输入框值改变事件
  infoChange(Qrcode) {
    const http$ = this.UnloadApi.ScanMateriel(Qrcode, this.planPositionId, this.planId, this.containerId, this.projectId);
    http$.subscribe((res: any) => {
      if (res.state) {
        for (var i = 0; i < this.ele.nativeElement.querySelectorAll(".hides").length; i++) {
          this.ele.nativeElement.querySelectorAll(".hides")[i].removeAttribute("hidden");
          this.ele.nativeElement.querySelector("#scanmaterielbtn").click();
        }
        this.materielAndcontainerInfo = res;
        this.containerbearweightNow = res.containerbearweightNow;
        this.MaterielvolumeSum = res.MaterielvolumeSum;
        this.disTag = false;
        this.bizTag=res.bizTag;
      } else {
        this.showMsg(res.msg);
        this.ele.nativeElement.querySelector("#scanmaterielbtn").click();
        this.ele.nativeElement.querySelector("#scanmateriel").focus();
      }

    });
  }

  toScan() {
    this.barcodeScanner.scan({showTorchButton:true,resultDisplayDuration:0}).then((data) => {
      if (data.text.length!=0){
        this.qrcode=data.text;
        const http$ = this.UnloadApi.ScanMateriel(data.text, this.planPositionId, this.planId, this.containerId, this.projectId);
        http$.subscribe((res: any) => {
          if (res.state) {
            for (var i = 0; i < this.ele.nativeElement.querySelectorAll(".hides").length; i++) {
              this.ele.nativeElement.querySelectorAll(".hides")[i].removeAttribute("hidden");
              //this.ele.nativeElement.querySelector("#scanmaterielbtn").click();
            }
            this.materielAndcontainerInfo = res;
            this.takePhoto=res.takePhoto;
            this.containerbearweightNow = res.containerbearweightNow;
            this.MaterielvolumeSum = res.MaterielvolumeSum;
            this.disTag = false;
            this.bizTag=res.bizTag;
            //此处判断强制拍照提示
            if(this.takePhoto=="0"){
              this.showMsg("货物属性为强制拍照,请拍照上传!");
              this.navCtrl.push("ForcedPhoto",{
                "qrcode": this.qrcode,
                "planPositionId": this.planPositionId,
                "bizTag":this.bizTag
              });
            }
          } else {
            this.showMsg(res.msg);
           // this.ele.nativeElement.querySelector("#scanmaterielbtn").click();
           // this.ele.nativeElement.querySelector("#scanmateriel").focus();
          }
        });

      }else{
        this.fixshowMsg("请扫描正确的二维码!");
      }
    }, (err)=> {
      this.showMsg("扫描出现异常" + err);
    });
  }

  scan() {
   // this.ele.nativeElement.querySelector("#scanmateriel").focus();
    this.ele.nativeElement.querySelector("#scanmaterielbtn").click();
  }

//点击取消返回
  goBack() {
   //this.ele.nativeElement.querySelector("[ng-reflect-klass='back-button']").click();
   this.navCtrl.pop();
  }

  showMsg(msg: string) {
    var toast = this.toastCtrl.create({
      message: msg,
      duration: 3000,
      position: 'bottom'
    });
    toast.present();
  }

  fixshowMsg(msg: string) {
    this.LeaveTag = false;
    var toast = this.toastCtrl.create({
      message: msg,
      duration: 3000,
      position: 'bottom'
    });
    toast.present();
    setTimeout(() => this.LeaveTag = true, 1000);
  }


}
